package com.jilian.powerstation.interceptor;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.jilian.powerstation.Constant;
import com.jilian.powerstation.utils.SPUtil;


import java.io.IOException;
import java.util.List;
import java.util.TimeZone;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 非首次请求的处理
 *
 * @author : weishixiong
 * @create : 18/05/03
 */
public class AddCookiesInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        String cookieStr = SPUtil.getData(Constant.SP_VALUE.SP, Constant.SP_VALUE.SESSION_ID, String.class, null);
        String token = SPUtil.getData(Constant.SP_VALUE.SP, Constant.SP_VALUE.TOKEN, String.class, null);
        List<String> cookies = JSONObject.parseArray(cookieStr, String.class);
        if (cookies != null) {
            for (String cookie : cookies) {
                builder.addHeader("Cookie", cookie);
                Log.v("OkHttp", "Adding Header: " + cookie); // This is done so I know which headers are being added; this interceptor is used after the normal logging of OkHttp
            }
        }
        if (!TextUtils.isEmpty(token)) {
            builder.addHeader("token", token);
        }

        builder.addHeader("timeZone", getCurrentTimeZone());

        Log.e("AddCookiesInterceptor", "intercept: " + getCurrentTimeZone());

        return chain.proceed(builder.build());
    }

    public static String getCurrentTimeZone() {
        TimeZone tz = TimeZone.getDefault();
      //  return tz.getID();
        return createGmtOffsetString(true, true, tz.getRawOffset());
    }

    public static String createGmtOffsetString(boolean includeGmt, boolean includeMinuteSeparator, int offsetMillis) {
        int offsetMinutes = offsetMillis / 60000;
        char sign = '+';
        if (offsetMinutes < 0) {
            sign = '-';
            offsetMinutes = -offsetMinutes;
        }
        StringBuilder builder = new StringBuilder(9);
        if (includeGmt) {
            builder.append("GMT");
        }
        builder.append(sign);
        appendNumber(builder, 2, offsetMinutes / 60);
//        if (includeMinuteSeparator) {
//            builder.append(':');
//        }
//        appendNumber(builder, 2, offsetMinutes % 60);
        return builder.toString();
    }

    private static void appendNumber(StringBuilder builder, int count, int value) {
        String string = Integer.toString(value);
//        for (int i = 0; i < count - string.length(); i++) {
//            builder.append('0');
//        }
        builder.append(string);
    }
}


